"""
对selenium进行二次封装
Base:
    初始化浏览器驱动对象
    退出浏览器
    元素定位
    请求目标网址
"""
import time
from appium.webdriver.common.touch_action import TouchAction
from appium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class Open_App:
    def open_app(appPackage: str, appActivity: str, platformName='Android', platformVersion='8.1.0'
                 , deviceName='192.168.137.219:9999'):
        desired_caps = {
            'platformName': platformName,
            'platformVersion': platformVersion,
            'deviceName': deviceName,
            'appPackage': appPackage,
            'appActivity': appActivity,
            'noReset': True
        }
        driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
        return driver


class Base(Open_App):
    def __init__(self, appPackage: str, appActivity: str):
        self.driver = Open_App.open_app(appPackage, appActivity)
        windows_size = self.driver.get_window_size()
        self.width = windows_size['width']
        self.height = windows_size['height']

    def find_element(self, locator, timeout=5, poll_frequency=0.5):
        """
        定位单个元素
        :param locator: 定位器
        :param timeout: 超时时间
        :param poll_frequency: 间隔时间
        :return: WebElement对象|None
        """
        try:
            return WebDriverWait(self.driver, timeout, poll_frequency) \
                .until(EC.presence_of_element_located(locator), message='元素定位失败')
        except TimeoutException as e:
            print(e)
            return None

    def find_elements(self, locator, timeout=5, poll_frequency=0.5):
        """
        定位多个元素
        :param locator: 定位器
        :param timeout: 超时时间
        :param poll_frequency: 间隔时间
        :return: WebElement对象|None
        """
        try:
            return WebDriverWait(self.driver, timeout, poll_frequency) \
                .until(EC.presence_of_all_elements_located(locator), message='元素定位失败')
        except TimeoutException as e:
            print(e)
            return []

    def click(self, locator):
        """点击操作"""
        element = self.find_element(locator)
        if element:
            element.click()

    def send_keys(self, locator, content):
        """
        输入框输入内容
        :param locator: 定位器
        :param content: 输入内容
        :return:
        """
        element = self.find_element(locator)
        if element:
            element.send_keys(content)

    def get_text(self, locator):
        element = self.find_element(locator)
        if element:
            return element.text

    def get_attribute(self, locator, name='outerHTML'):
        """
        获取标签属性值
        :param locator: 定位器
        :param name: 属性名
        :return:
        """
        element = self.find_element(locator)

        if element:
            return element.get_attribute(name)

    def tap(self, x: int, y: int):
        TouchAction(self.driver).tap(x=x, y=y).perform()

    def press(self, x, y, milliseconds=600):
        TouchAction(self.driver).press(x=x,y=y).wait(milliseconds).release().perform()

    def slide_up(self, duration=1000):
        x = self.width * 0.5
        start_y = self.height * 0.8
        end_y = self.height * 0.2
        TouchAction(self.driver).long_press(x=x, y=start_y, duration=duration).move_to(x=x, y=end_y).release().perform()

    def clear(self,locator):
        element = self.find_element(locator=locator)
        if element:
            element.clear()

    def quit(self, seconds=0):
        time.sleep(seconds)
        self.driver.quit()


if __name__ == '__main__':
    base = Base(appPackage='com.nearme.gamecenter', appActivity='.ui.activity.SplashActivity ')
    time.sleep(5)
    base.slide_up()
    base.quit()
